package com.aegis.manage.utils;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import com.aegis.core.model.AegisSession;
import com.aegis.core.utils.AegisUserUtil;
import com.aegis.manage.config.ManageProperties;
import com.aegis.spring.config.AegisAppProperties;
import com.aegis.spring.config.AegisUserProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author renogo
 *
 * @Version 1.0
 * @Descritube 获取用户登录信息的工具
 */
@Component
public class AuthUtil {

    private static ManageProperties manageProperties;

    private static AegisAppProperties aegisAppProperties;

    @Autowired
    public void setTokenProperties(ManageProperties manageProperties, AegisAppProperties aegisAppProperties) {
        this.manageProperties = manageProperties;
        this.aegisAppProperties = aegisAppProperties;
    }

    /**
     * 获取当前登录的用户id
     * @return
     */
    public static String getLoginUserId() {
        return (String) StpUtil.getLoginId();
    }

    /**
     * 获取当前登录的用户id
     * @return
     */
    public static AegisSession getLoginUserInfo() {
        return AegisUserUtil.getLoginUserInfo();
    }


    /**
     * 当前用户是否是管理员
     *
     * @return
     */
    public static boolean isAdmin() {
        return isAdmin((String) AegisUserUtil.getLoginUserId());
    }

    /**
     *
     * @return
     */
    public static List<String> getAdminKey() {
        return manageProperties.getAdminRoles();
    }

    /**
     * 某个用户是否是管理员
     *
     * @param userId
     * @return
     */
    public static boolean isAdmin(String userId) {
        if (CollUtil.isEmpty(manageProperties.getAdminRoles())) {
            return false;
        }
        for (String adminRole : manageProperties.getAdminRoles()) {
            if (StpUtil.hasRoleOr(userId, adminRole)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 获取当前登录的用户的权限
     *
     * @return
     */
    public static List<String> getPermission() {
        return StpUtil.getPermissionList();
    }

    /**
     * 获取某个用户的权限
     *
     * @param userId
     * @return
     */
    public static List<String> getPermission(String userId) {
        return StpUtil.getPermissionList(userId);
    }


    /**
     * 获取当前用户的角色
     *
     * @return
     */
    public static List<String> getRoles() {
        return StpUtil.getRoleList();
    }

    /**
     * 获取当前用户的角色
     *
     * @return
     */
    public static List<String> getRoles(String userId) {
        return StpUtil.getRoleList(userId);
    }


    /**
     * 获得请求头的appId
     *
     * @return
     */
    public static String getHeaderAppId() {
        return getHeader(aegisAppProperties.getHeaderNameAppId());
    }

    /**
     * @param key
     * @return
     */
    public static String getHeader(String key) {
        HttpServletRequest request = getHttpServletRequest();
        if (request != null) {
            return request.getHeader(key);
        }
        return null;
    }

    /**
     * 获取当前 HttpServletRequest
     *
     * @return HttpServletRequest 对象
     */
    private static HttpServletRequest getHttpServletRequest() {
        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes != null ? attributes.getRequest() : null;
    }

}
